package com.verbetia.mbrainz.tags;


import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;




public class AlbumTracksReader {


	static final String URL_PREFIX = "http://ws.audioscrobbler.com/1.0/album/";
	static final String URL_SUBFIX = "/info.xml";
	
	AlbumTracksXMLReader lib;

	
	public static void main(String[] args) {
		
		AlbumTracksReader tr = new AlbumTracksReader("Los Piojos","Maquina de Sangre");
		
		
		AlbumItem a = tr.getAlbumItem();
		
		int i = 0;
		for (String t : a.getTracks())
		{
			
			System.out.println((++i) + "." + t);
		}

		
	}
/*
 
*/
	
	public AlbumTracksReader(String artist, String album)
	{
        String url = URL_PREFIX + artist + "/" + album + URL_SUBFIX;
        System.out.println(url);
        
        lib = new AlbumTracksXMLReader();
     
		 try{
	            SAXParserFactory spf=SAXParserFactory.newInstance();
	            SAXParser sp = spf.newSAXParser();	            
	           	sp.parse(url, lib );
	        }catch(ParserConfigurationException e){
	        	System.err.println("error de  parseo");
	        }catch(SAXException e2){
	        	System.err.println("error de  sax : " + e2.getStackTrace());
	        } catch (IOException e3) {
				// TODO Auto-generated catch block
	        	System.err.println("error de  io : " + e3.getMessage() );
	        }

			
		
	}
	
	public AlbumItem getAlbumItem()
	{
		return lib.getAlbumItem();
	}



	private class AlbumTracksXMLReader extends DefaultHandler {
  	
	 String contenido="";  // cadena para almacenar el contenido de un tag
	 TagItem item = null;
	 String track;
	 AlbumItem album = new AlbumItem();
	 	 	 
	 
	 public AlbumItem getAlbumItem()
	 {
		 return album;
		 
	 }
	 
	 public void startDocument() throws SAXException{ }
	 public void endDocument()throws SAXException{
		 System.out.println("end doc");
	 		
	 }

	/* Esta funcion el llamada cuando se produce el evento 
	 * de(non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
	System.out.println("he encontrado una nueva etiqueta : " + qName);
	
		if (qName.equals("track"))
		{			
				album.addTrack(attributes.getValue("title"));
		}
		
		
   }

	/* Esta funcion es llamada cuando ve el contenido de un tag
	 * (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
   public void characters(char buf[], int offset, int len)
   throws SAXException
   {
   	contenido = new String(buf, offset, len);
   }
   
   
   
   /* 
    * (non-Javadoc)
    * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
    */
   public void endElement(String uri, String localName, String qName) {
	  // System.out.println(qName + " : " + contenido);
	   
	   if (qName.equals("releasedate"))
	   	album.setRelease(contenido);

 	   if (qName.equals("medium"))
		   	album.setImage(contenido);

    }
 }


}